package cn.edu.zafu.bigdata.x.zhaobolei.Sixth;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

public class ZZTextFile {

    public static void writeToFile(String fileName, String content) {
        try {
            FileWriter writer = new FileWriter(fileName);
            writer.write(content);
            writer.close();
            System.out.println("文本已成功写入文件：" + fileName);
        } catch (IOException e) {
            System.out.println("写入文件时出错：" + e.getMessage());
        }
    }

    public static void readFromFile(String fileName) {
        try {
            String content = new String(Files.readAllBytes(Paths.get(fileName)));
            System.out.println("文件内容如下：");
            System.out.println(content);
        } catch (IOException e) {
            System.out.println("读取文件时出错：" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你的学号：");
        String studentId = scanner.nextLine();

        String fileLocation = "files/" + studentId + ".txt";

        System.out.println("请输入要保存到文件的文本内容：");
        String inputText = scanner.nextLine();

        writeToFile(fileLocation, inputText);

        readFromFile(fileLocation);

        scanner.close();
    }
}